﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebApplication1.Classes;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections.Generic;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        private string Сon = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
        private int value;
        private String role;
        private string name;
        private string surname;
        private int id;


        protected void Connection(string c)
        {
            try
            {
                SqlConnection connection = new SqlConnection(c);
                connection.Open();
            }
            catch (SqlException ex)
            {
                Server.Transfer("Error.aspx?error=" + ex.Message);

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Label3.Visible = true;
            Label5.Visible = true;
            id = Convert.ToInt32(Session["id"]);
            role = Convert.ToString(Session["role"]);
            name = Convert.ToString(Session["name"]);
            surname = Convert.ToString(Session["surname"]);
            Session.Remove("id");
            Session.Remove("role");
            Session.Remove("name");
            Session.Remove("surname");
            Session["id"] = id;
            Session["name"] = name;
            Session["surname"] = surname;
            Session["role"] = role;
            Label3.Text = ("Здравствуйте, " + name + " " + surname + "!!!");
            Label5.Text = ("Вы зашли под правами " + role);
            if (role == "Агент")
            {

                ButtonNewContract.Visible = true;
                TextBoxInfo.Visible = false;
            }
            if (role == "Администратор")
            {
                TextBoxInfo.Visible = false;
                ButtonUsers.Visible = true;
                ButtonNewContract.Visible = true;
               
            }
            if (role == "Пользователь")
            {

                ButtonUsers.Visible = false;
                TextBoxInData.Visible = false;
                TextBoxOutData.Visible = false;
                ButtonDateFilter.Visible = false;
                ButtonNewContract.Visible = false;
                GridView1.AutoGenerateEditButton = false;
                GridView1.AutoGenerateDeleteButton = false;
                GridView1.Visible = false;
                ButtonSearch.Visible = false;
                CalendarIn.Visible = false;
                CalendarOut.Visible = false;
            }
        }
        protected void ButtonDateFilterClick(object sender, EventArgs e)
        {  String Сon = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
            SqlConnection connection = new SqlConnection(Con);
            try
            {
               
                GridViewSearch.Visible = true;
                GridView1.Visible = false;
                connection.Open();
                DateTime date_out;
                DateTime date_in;
                date_in = Convert.ToDateTime(TextBoxInData.Text);
                date_out = Convert.ToDateTime(TextBoxOutData.Text);

                string strQuery = "SELECT * FROM Contract where Date BETWEEN " + "'" + date_in + "'" + " and " + "'" + date_out + "'";
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter();
                sda.SelectCommand = new SqlCommand(strQuery, connection);
                sda.Fill(ds, "Contract");
                GridViewSearch.DataSource = ds.Tables["Contract"];
                this.DataBind();
                connection.Close();
            }
            catch (Exception ex)
            {
                connection.Close();
                Server.Transfer("Error.aspx?error=" + ex.Message);
            }
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            value = Convert.ToInt32(GridView1.SelectedDataKey.Value);
            Session["con"] = value;
            Server.Transfer("details.aspx");
        }

        protected void ButtonNewContractClick(object sender, EventArgs e)
        {
            Server.Transfer("client.aspx");
        }

        protected void ButtonUsersClick(object sender, EventArgs e)
        {
            Server.Transfer("Users.aspx");
        }

        protected void ButtonSearchClick(object sender, EventArgs e)
        {
            Server.Transfer("Poisk.aspx");
        }



        protected void GridViewSearchSelectedIndexChanged(object sender, EventArgs e)
        {
            value = Convert.ToInt32(GridViewSearch.SelectedDataKey.Value);
            Session["con"] = value;
            Server.Transfer("details.aspx");
        }

        protected void CalendarInSelectionChanged(object sender, EventArgs e)
        {
            TextBoxInData.Text = Convert.ToString(CalendarIn.SelectedDate);
        }

        protected void CalendarOutSelectionChanged(object sender, EventArgs e)
        {
            TextBoxOutData.Text = Convert.ToString(CalendarOut.SelectedDate);
        }

        protected void ButtonCefficientsClick(object sender, EventArgs e)
        {
            Server.Transfer("insuranceRates.aspx");
        }


    }
}
